package com.smart.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.smart.dao.UserDAO;
import com.smart.model.House;
import com.smart.model.UnverifiedUser;
import com.smart.model.User;
import com.smart.util.DBUtil;

public class UserService {
	static UserDAO userDAO = null;

	static {
		userDAO = new UserDAO();
	}

	public void add(User u, int communityId) throws SQLException {
		userDAO.addUser(u, communityId);
	}

	public void del(String open_id) throws SQLException {
		System.out.println("add3");
		userDAO.delUser(open_id);
		System.out.println("add4");
	}

	public User userLogin(String phone, String password) throws SQLException {
		return userDAO.userLogin(phone, password);
	}

	public boolean userRegister(String phone, String password) throws SQLException {
		return userDAO.userRegister(phone, password);
	}

	// 用户认证
	public boolean userReg(House house, User user) {
		boolean success = true;
		HouseService houseSer = new HouseService();
		int building_no = house.getBuilding_id();
		int room_no = house.getRoom_no();
		int houseID = houseSer.getHouseId(building_no, room_no);
		user.setHouseID(houseID);
		success = userDAO.updateUserByOpenId(user);
		System.out.println("userReg method:" + success);
		return success;
	}

	public List<UnverifiedUser> getUnverifiedUser() throws SQLException {
		return userDAO.getUnverifiedUser();
	}

	public boolean updateUser(int userID, int verified) {
		return userDAO.updateUserByVerifyStatus(userID, verified);
	}

	public List<User> selectUser(int communityId) throws SQLException {
		List<User> list = userDAO.selectUser(communityId);
		return list;
	}

	public int getIdByTelephoneNum(String telephoneNum, int communityId) {
		int id = -1;
		try {
			id = userDAO.getIdByTelephoneNum(telephoneNum, communityId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return id;
	}

	public boolean regOper(String openId, String nickName, String telephoneNum, int communityId) {
		boolean success = false;
		try {
			success = userDAO.regOper(openId, nickName, telephoneNum, communityId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return success;
	}

	public List<User> getUserList(String name, String telephoneNum, String buildingNo, String roomNo, int pageIndex,
			int communityId) {
		List<User> userList = null;
		try {
			userList = userDAO.getUserList(name, telephoneNum, buildingNo, roomNo, pageIndex, communityId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userList;
	}

	// 获取总数
	public int getUserCount(String name, String telephoneNum, String buildingNo, String roomNo, int communityId) {
		int count = -1;
		try {
			count = userDAO.getUserCount(name, telephoneNum, buildingNo, roomNo, communityId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	// 添加用户
	public boolean addUser(String name, String telephoneNum, int type, int buildingNo, int roomNo, int communityId) {
		HouseService houseSer = new HouseService();
		boolean res = true;
		int houseId;
		try {
			houseId = houseSer.getHouseId(buildingNo, roomNo);
			if (houseId == -1) {
				System.out.println("输入的楼宇号或者房间号存在问题！！！");
				res = false;
			} else
				res = userDAO.addUser(name, telephoneNum, houseId, type, communityId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	// 通过ID删除用户
	public boolean deleteUserById(int id) {
		boolean success = false;
		try {
			success = userDAO.deleteUserById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return success;
	}

	// 提供给管理员修改用户信息
	public boolean updateUser(int id, String telephoneNum, String name, int type, int houseId) {
		boolean res = false;
		try {
			res = userDAO.updateUser(id, telephoneNum, name, type, houseId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	// 根据openId检查是否已经是认证用户
	public int getVerifiedByOpenId(String openId) {
		int verified = 0;
		try {
			verified = userDAO.getVerifiedByOpenId(openId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return verified;
	}

}
